package sk.stuba.fiit.foo07.genex.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import sk.stuba.fiit.foo07.genex.beans.Category;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class NewCategoryDialog extends javax.swing.JDialog {

    private static final long serialVersionUID = 3656493253626676672L;
    private JLabel jLabel2;
    private JButton jBtnOK;
    private JButton jBtnCancel;
    private JTextArea jTxtDesc;
    private JScrollPane jScrollDec;
    private JTextField jTxtName;
    private JLabel jLabel1;

    private Category ret;
    private JSeparator jSeparator1;

    public NewCategoryDialog(JFrame frame, Category toEdit) {
        super(frame);
        initGUI();

        if (toEdit != null) {
            setTitle("Upraviť kategóriu");
            ret = toEdit;
            jTxtName.setText(ret.getName());
            jTxtDesc.setText(ret.getDescription());
            jTxtDesc.setCaretPosition(0);
            jTxtName.setCaretPosition(0);
        } else
            ret = new Category();
    }

    private void initGUI() {
        try {
            {
                this.setTitle("Nová kategória");
                getContentPane().setLayout(null);
                this.setResizable(false);
                this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                setLocationByPlatform(true);
                {
                    jLabel1 = new JLabel();
                    getContentPane().add(jLabel1);
                    jLabel1.setText("Názov kategórie");
                    jLabel1.setBounds(12, 12, 112, 14);
                    jLabel1.setForeground(new java.awt.Color(100, 0, 0));
                }
                {
                    jTxtName = new JTextField();
                    getContentPane().add(jTxtName);
                    jTxtName.setBounds(136, 9, 144, 21);
                    jTxtName.setBackground(new java.awt.Color(255, 255, 240));
                    jTxtName.setForeground(new java.awt.Color(0, 0, 255));
                }
                {
                    jLabel2 = new JLabel();
                    getContentPane().add(jLabel2);
                    jLabel2.setText("Popis kategórie");
                    jLabel2.setBounds(12, 32, 112, 14);
                    jLabel2.setForeground(new java.awt.Color(100, 0, 0));
                }
                {
                    jScrollDec = new JScrollPane();
                    getContentPane().add(jScrollDec);
                    jScrollDec.setBounds(12, 52, 268, 93);
                    {
                        jTxtDesc = new JTextArea();
                        jScrollDec.setViewportView(jTxtDesc);
                        jTxtDesc
                                .setBackground(new java.awt.Color(255, 255, 240));
                        jTxtDesc.setForeground(new java.awt.Color(0, 0, 255));
                    }
                }
                {
                    jBtnCancel = new JButton();
                    getContentPane().add(jBtnCancel);
                    jBtnCancel.setText("Cancel");
                    jBtnCancel.setBounds(204, 165, 77, 21);
                    jBtnCancel.setMnemonic(java.awt.event.KeyEvent.VK_C);
                    jBtnCancel.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent evt) {
                            jBtnCancelActionPerformed(evt);
                        }
                    });
                }
                {
                    jBtnOK = new JButton();
                    getContentPane().add(jBtnOK);
                    jBtnOK.setText("OK");
                    jBtnOK.setBounds(122, 165, 77, 21);
                    jBtnOK.setMnemonic(java.awt.event.KeyEvent.VK_O);
                    jBtnOK.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent evt) {
                            jBtnOKActionPerformed(evt);
                        }
                    });
                }
                {
                    jSeparator1 = new JSeparator();
                    getContentPane().add(jSeparator1);
                    jSeparator1.setBounds(12, 151, 270, 10);
                }
            }
            this.setSize(300, 232);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Category showDialog() {
        this.setModalityType(ModalityType.DOCUMENT_MODAL);
        this.setVisible(true);

        return ret;
    }

    private void jBtnCancelActionPerformed(ActionEvent evt) {
        ret = null;
        setVisible(false);
    }

    private void jBtnOKActionPerformed(ActionEvent evt) {
        if (jTxtName.getText().trim().length() == 0) {
            JOptionPane.showMessageDialog(this,
                    "Musíte zadať minimálne názov kategórie!", "Chyba",
                    JOptionPane.ERROR_MESSAGE);
            return;
        }

        if (jTxtName.getText().trim().length() > 50) {
            JOptionPane.showMessageDialog(this,
                    "Názov kategórie môže obsahovať maximálne 50 znakov.",
                    "Chyba", JOptionPane.ERROR_MESSAGE);
            return;
        }

        ret.setName(jTxtName.getText().trim());
        ret.setDescription(jTxtDesc.getText());
        setVisible(false);
    }
}
